package com.yyhh.helpcall.server;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.huawei.hms.push.HmsMessageService;
import com.huawei.hms.push.RemoteMessage;
import com.yyhh.helpcall.model.MessageModel;
import com.yyhh.helpcall.utils.AppUtils;
import com.yyhh.helpcall.utils.NetLog;
import com.umeng.analytics.MobclickAgent;
import java.util.HashMap;

public class HuaWeiPushService extends HmsMessageService {

    @Override
    public void onMessageReceived(RemoteMessage message) {
        super.onMessageReceived(message);
        if(message == null) return;
        NetLog.d("HuaWeiPushService.onMessageReceived" + "getCollapseKey: " + message.getCollapseKey()
                + "\n getData: " + message.getData()
                + "\n getFrom: " + message.getFrom()
                + "\n getTo: " + message.getTo()
                + "\n getMessageId: " + message.getMessageId()
                + "\n getSendTime: " + message.getSentTime()
                + "\n getDataMap: " + message.getDataOfMap()
                + "\n getMessageType: " + message.getMessageType()
                + "\n getTtl: " + message.getTtl()
                + "\n getToken: " + message.getToken());

        try {
            String data = message.getData();
            MessageModel messageModel = new Gson().fromJson(data, MessageModel.class);
            switch (messageModel.type) {
                case "call_phone":
                    if (!TextUtils.isEmpty(messageModel.data.phone)) {
                        AppUtils.callPhone(getApplicationContext(), messageModel.data.phone,messageModel.data.msg_id,"华为推送 ");
                        //发送事件
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("phone", messageModel.data.phone);
                        MobclickAgent.onEventObject(getApplicationContext(), "callPhone", map);
                    }
                    break;
                case "cancel_call":
                    AppUtils.endCall(getApplicationContext(),messageModel.data.msg_id,messageModel.data.phone,"华为推送 ");
                    //发送事件
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("phone", messageModel.data.phone);
                    MobclickAgent.onEventObject(getApplicationContext(), "cancleCall", map);
                    break;
                default:
                    break;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
